package com.mfhcd.facepay.utils;

import android.text.TextUtils;
import android.widget.EditText;

import java.util.regex.Pattern;

/**
 * @author Eiozzgh
 * @author xiaoxiaozrsh@gmail.com
 */
public class CheckUtils {
    public enum CheckType {
        PHONE, IMCODE, SMSCODE, IDCARD, BANKCARD, CH_NAME, LOGIN_NAME, PWD, TRADE_PWD
    }

    public static boolean checkInputEmpty(String toast, String input) {
        if (TextUtils.isEmpty(input)) {
            ToastUtil.showToast(toast);
            return false;
        }
        return true;
    }

    public static boolean checkInputEmpty(String[] toasts, String... inputs) {
        for (int i = 0; i < inputs.length; i++) {
            if (TextUtils.isEmpty(inputs[i])) {
                if (i < toasts.length)
                    ToastUtil.showToast(toasts[i]);
                return false;
            }
        }
        return true;
    }

    public static boolean checkInputEmpty(String[] toasts, EditText... inputs) {
        for (int i = 0; i < inputs.length; i++) {
            if (TextUtils.isEmpty(inputs[i].getText().toString().trim())) {
                if (i < toasts.length)
                    ToastUtil.showToast(toasts[i]);
                return false;
            }
        }
        return true;
    }

    public static boolean checkInputValid(CheckType checkType, String input) {
        return checkValid(checkType, input);
    }

    /**
     * 检查特殊项的输入
     */
    public static boolean checkInputValid(CheckType[] checkTypes, String... inputs) {
        for (int i = 0; i < inputs.length; i++) {
            if (!checkValid(checkTypes[i], inputs[i]))
                return false;
        }
        return true;
    }

    public static boolean checkValid(CheckType checkType, String input) {
        switch (checkType) {
            case PHONE:
                if (TextUtils.isEmpty(input) || !Pattern.matches("^[1]\\d{10}$", input)) {
                    ToastUtil.showToast("请输入正确的手机号码");
                    return false;
                }
                break;
            case IMCODE:
                if (TextUtils.isEmpty(input) || input.length() != 4) {
                    ToastUtil.showToast("请输入4位图形验证码");
                    return false;
                }
                break;
            case SMSCODE:
                if (TextUtils.isEmpty(input) || input.length() != 6) {
                    ToastUtil.showToast("请输入6位短信验证码");
                    return false;
                }
                break;
            case IDCARD:
                if (TextUtils.isEmpty(input) || !RxRegTool.validateIdCard(input)) {
                    ToastUtil.showToast("请输入有效的身份证号");
                    return false;
                }
                break;
            case BANKCARD:
                if (TextUtils.isEmpty(input) || !RxRegTool.isBankCard(input)) {
                    ToastUtil.showToast("请输入正确的银行卡号");
                    return false;
                }
                break;
            case CH_NAME:
                if (TextUtils.isEmpty(input) || !RxRegTool.isLegalName(input)) {
                    ToastUtil.showToast("请输入正确的姓名");
                    return false;
                }
                break;
            case LOGIN_NAME:
                if (TextUtils.isEmpty(input) || !RxRegTool.isLegalLoginName(input)) {
                    ToastUtil.showToast("登录名必须为6-18位，且必须以字母开头");
                    return false;
                }
                break;
            case PWD:
                if (TextUtils.isEmpty(input) || input.length() < 6 || input.length() > 18) {
                    ToastUtil.showToast("密码必须为6-18位");
                    return false;
                }
                break;
            case TRADE_PWD:
                if (TextUtils.isEmpty(input) || input.length() != 6) {
                    ToastUtil.showToast("请输入6位支付密码");
                    return false;
                }
                break;
        }
        return true;
    }
}
